package shuashuami.hb.com.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import org.json.JSONException;
import org.json.JSONObject;

import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.http.HttpCMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.share.ShareCUserInfo;

/**
 * Created by Administrator on 2017/6/20.
 */

public class CTIjiaoShenqingDialog extends Dialog {

    private String id;
    private Context activity;

    public CTIjiaoShenqingDialog(Context context, String type) {
        super(context, R.style.Dialog);
        this.activity = context;
        this.id = type;
        setCustomDialog();
    }

    private void setCustomDialog() {
        View mView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_tiaojiaoshenqing_layout, null);
        final EditText edtData = (EditText) mView.findViewById(R.id.edt_change_content_data);
        Button btnCancle = (Button) mView.findViewById(R.id.btn_change_content_cancle);
        Button btnEnter = (Button) mView.findViewById(R.id.btn_change_content_enter);
        btnCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        btnEnter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                recommendListc(edtData.getText().toString());
            }
        });
        super.setContentView(mView);
    }


    @Override
    public void setContentView(int layoutResID) {
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
    }

    @Override
    public void setContentView(View view) {
    }

    private void recommendListc(String content) {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                //    openActivityAndCloseThis(HbRegistered2Act.class);

                Message message = handler.obtainMessage(1);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(activity);
        HttpCMethods.recommendApply(listener, shareCUserInfo.getId(), id, content
        );
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 200) { //成功
                            ToastUtil.showShort(activity, jsonObject.getString("message"));
                        } else {
                            ToastUtil.showShort(activity, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        ToastUtil.showShort(activity, "数据解析错误");
                    }


                    break;
            }

            super.handleMessage(msg);
        }
    };
}
